﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class RoleInfoController : ControllerBase
    {
        IFreeSql db;
        public RoleInfoController(IFreeSql _db)
        {
            db = _db;
        }

        /// <summary>
        /// 查询角色管理
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">条数</param>
        /// <param name="rolename">角色名称</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInfo(int page, int limit, string? rolename)
        {
            var list = db.Select<Models.Roleinfo>()
                .Where(x => ((string.IsNullOrEmpty(rolename)) || (x.Rolename.Contains(rolename))) && x.Isdelete == false)
                .Count(out var total)
                .Page(page, limit)
                .ToList();
            var json = new
            {
                code = 200,
                data = list,
                total = total
            };
            return Ok(json);
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromBody] Models.Roleinfo info)
        {
            try
            {
                info.Createtime = DateTime.Now;
                info.Isdelete = false;
                int list = db.Insert(info).ExecuteAffrows();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(ex);
            }
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Update([FromBody] Models.Roleinfo info)
        {
            try
            {
                int list = db.Update<Models.Roleinfo>().SetSource(info).ExecuteAffrows();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="aid">ID</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(int roleid)
        {
            try
            {
                int list = db.Update<Models.Roleinfo>(roleid)
                    .Set(x => x.Isdelete == true)
                    .ExecuteAffrows();
                return Ok(list > 0);
            }
            catch (Exception ex)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 获取角色信息绑定下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("getRoleInfo")]
        public IActionResult getRoleInfo()
        {
            var list = db.Select<Models.Roleinfo>()
                .Where(x => x.Isdelete == false)
                .ToList(x => new
                {
                    x.Roleid,
                    x.Rolename
                });
            return Ok(list);
        }
    }
}
